﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 诺贝达往季复盘打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 诺贝达往季复盘打印()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                string sqldm = @"exec [zdy_getReplayPrint] '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtmain = DBHelper.ExecuteDataTable(sqldm);
                if (dtmain.Rows.Count > 0)
                {
                    NewUsb.FindUSBPrinter();
                    for (int k = 0; k < NewUsb.USBPortCount; k++)
                    {
                        if (NewUsb.LinkUSB(k))
                        {
                            SendData2USB("SIZE 50 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                            SendData2USB("GAP 6 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                            SendData2USB("CLS\r\n");//清空缓冲区
                            SendData2USB("DENSITY 7\r\n");//打印浓度
                            SendData2USB("REFERENCE 0,0\r\n");
                            SendData2USB("TEXT 2,10,\"TSS20.BF2\",0,1,1,\"中类\"\r\n");
                            SendData2USB("TEXT 60,10,\"TSS20.BF2\",0,1,1,\"" + dtmain.Rows[0]["中类"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 2,40,\"TSS20.BF2\",0,1,1,\"货号\"\r\n");
                            SendData2USB("TEXT 60,40,\"TSS20.BF2\",0,1,1,\"" + dtmain.Rows[0]["SKC"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 2,70,\"TSS20.BF2\",0,1,1,\"下单总数\"\r\n");
                            SendData2USB("TEXT 100,70,\"TSS24.BF2\",0,1,1,\"" + dtmain.Rows[0]["下单件数"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 160,70,\"TSS20.BF2\",0,1,1,\"销售件数\"\r\n");
                            SendData2USB("TEXT 250,70,\"TSS24.BF2\",0,1,1,\"" + dtmain.Rows[0]["销售"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 2,100,\"TSS20.BF2\",0,1,1,\"售罄\"\r\n");
                            SendData2USB("TEXT 80,100,\"TSS24.BF2\",0,1,1,\"" + dtmain.Rows[0]["售罄率"].ToString() + "\"\r\n");

                            string sqlmx = @"exec [zdy_getReplayPrint_mx] '{0}'";
                            sqlmx = string.Format(sqlmx, dm);
                            DataTable dtmx = DBHelper.ExecuteDataTable(sqlmx);
                            SendData2USB("TEXT 2,160,\"TSS24.BF2\",0,1,1,\"店仓名称\"\r\n");
                            SendData2USB("TEXT 250,160,\"TSS24.BF2\",0,1,1,\"销售\"\r\n");
                            int gd = 160;
                            for (int x = 0; x < dtmx.Rows.Count; x++)
                            {
                                gd = gd + 30;
                                SendData2USB("TEXT 2," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtmx.Rows[x]["店仓名称"].ToString() + "\"\r\n");
                                SendData2USB("TEXT 250," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtmx.Rows[x]["销售"].ToString() + "\"\r\n");

                            }
                            SendData2USB("PRINT 1\r\n");
                            NewUsb.CloseUSBPort();

                            textBox1.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("请确定是否连接上打印机");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("条码不存在");
                }
            }
        }

        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }

        private void 诺贝达往季复盘打印_Load(object sender, EventArgs e)
        {

        }
    }
}
